import Vue from 'vue'
import axios from 'axios'

axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'; //配置请求头

const axiosFun = {};

let uploadFileType = "multipart/form-data";

//返回一个Promise(发送post请求),
axiosFun.fetchPost = (url, params) => {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(response => {
                if (response.data.Result === "100002") { //未登录
                    window.Wui.Toast.show(response.data.msg, 'middle');
                    setTimeout(function() {
                        location.href = '#/login';
                    }, 3000);
                } else {
                    if (url != "/article/updateStar" && url.indexOf("/user/updateintegral") < 0 && url != "/ex/addexlog") {
                        window.Wui.Toast.show(response.data.msg, 'middle');
                    }
                    if (url == '/user/register' && response.data.Result == 'success') {
                        Vue.prototype.$wx.closeWindow();
                        //location.href = 'https://mp.weixin.qq.com/mp/homepage?__biz=MzU1NDcwODI0MQ==&hid=2&sn=2e584d3bd438302f0ba11c2d9cb0f58a&scene=18#wechat_redirect';
                    }
                    if (response.data.Result == 'success' && url != "/article/updateStar" && url != '/user/register') {
                        //setTimeout(location.reload(), 1500);
                    }
                    resolve(response.data);
                }

            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            })
    })
}



axios.defaults.timeout = 60000;
/**
 * 上传文件的请求
 * @param url
 * @returns {AxiosPromise}
 */
axiosFun.uploadFile = (data) => {
    return new Promise((resolve, reject) => {
        let config = {
            //请求的接口，在请求的时候，如axios.get(url,config);这里的url会覆盖掉config中的url
            url: '/case/uploadcaseimg',
            //基础url前缀
            //baseURL: ,
            //   transformResponse: [function (data1) {
            //     var data = data1;
            //     if (typeof data1 == "string") {
            //       data = JSON.parse(data1);
            //     }
            //     return data;
            //   }],
            //请求头信息
            headers: { 'Content-Type': uploadFileType },

            //跨域请求时是否需要使用凭证
            withCredentials: true,
            // 返回数据类型
            responseType: 'json', //default
        };
        axios.post(config.url, data, config).then(response => {
            resolve(response);
        });
    });
}


axiosFun.AutoUser = (data) => {
    alert(11);
}

export default axiosFun;